
package com.yuan.application;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.yuan.control.SoundControl;
import com.yuan.prefence.Actions;
import com.yuan.prefence.FileConstants;
import com.yuan.receiver.NetStateReceiver;
import com.yuan.unit.CommUtils;
import com.yuan.unit.FileUtils;
import com.yuan.unit.PreferenceUtil;
import com.yuan.unit.SystemInfo;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;

import java.io.File;

/**
 * 本程序的Application
 * 
 * @author YUAN
 */
public class GraduateApplicationContext extends Application implements OnMyDestroyListener {
    public static GraduateApplicationContext INSTANCE;
    private NetStateReceiver receiver = null;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        initSystem();
        if (SystemInfo.DEBUG) {
            CommUtils.dumpSystemInfo();
        }
        initReceiver();
        ApplicationCleaner.getInstance().registerApplicationDestroyListener(this);
        initImageLoader(this);
    }

    @SuppressLint("CommitPrefEdits")
    private void initSystem() {
        // TODO Auto-generated method stub
        PreferenceUtil.getInstance().init(this);
        final SharedPreferences pref = PreferenceUtil.getInstance().pref();
        final Editor editor = pref.edit();
        final Resources resources = getResources();
        SystemInfo.init(this, pref, editor, resources);
        FileUtils.mkdirs(new File(FileConstants.LOCAL_PATH_BASE));
        FileUtils.mkdirs(new File(FileConstants.LOCAL_PATH_IMAGES));
        FileUtils.mkdirs(new File(FileConstants.LOCAL_PATH_SERIALIZABLE));
        SoundControl.getInstance(getApplicationContext());
    }

    private void initReceiver() {
        // TODO Auto-generated method stub
        if (receiver == null) {
            receiver = new NetStateReceiver();
            IntentFilter filter = null;
            if (filter == null) {
                filter = new IntentFilter();
                filter.addAction(Actions.RECEIVER.NETRECEIVER);
                filter.setPriority(Integer.MAX_VALUE);
            }
            registerReceiver(receiver, filter);
        }
    }

    @Override
    public void onMyDestroy() {
        // TODO Auto-generated method stub
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
        receiver = null;
    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);
    }
}
